package com.csdj.service.impl;

import com.csdj.dao.InformationsMapper;
import com.csdj.dao.RepliesMapper;
import com.csdj.exception.BusinessException;
import com.csdj.service.InformationsService;
import com.csdj.vo.Informations;
import com.csdj.vo.Replies;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @ClassName: InformationsServiceImpl
 * @Author: ZouLi
 * @Description: 资讯业务逻辑接口实现类
 * @Date: 2022/9/7 17:43
 **/
@Service(value = "informationsService")
public class InformationsServiceImpl implements InformationsService {
    // 注入dao
    @Autowired(required = false)
    private InformationsMapper informationsMapper;
    @Autowired(required = false)
    private RepliesMapper repliesMapper;

    /**
     * @Description: 查询全部资讯信息
     * @Param []
     * @Return java.util.List<com.csdj.vo.Informations>
     * @Throws
     **/
    @Override
    public List<Informations> selectAllInformation() {
        return informationsMapper.selectAllInformation();
    }

    /**
     * @Description: 根据编号查询资讯信息
     * @Param [id]
     * @Return com.csdj.vo.Informations
     * @Throws
     **/
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Informations selectInformationById(Integer id) {
        // 添加查看次数
        int row = informationsMapper.addViewCount(id);
        if (row>0){
            return informationsMapper.selectInformationById(id);
        }else{
            throw new BusinessException("添加查看数错误！");
        }
    }

    /**
     * @Description: 添加回复
     * @Param [replies]
     * @Return boolean
     * @Throws
     **/
    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public boolean addReplie(Replies replies) {
        int irow = informationsMapper.addReplieCount(replies.getInfoid());
        if (irow>0){
            int rrow = repliesMapper.insertSelective(replies);
            return irow+rrow>1;
        }else{
            throw new BusinessException("添加回复数出错！");
        }
    }
}
